package org.malred.hotelsystem.activities.admin;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.malred.hotelsystem.R;
import org.malred.hotelsystem.activities.BaseActivity;
import org.malred.hotelsystem.dao.HotelDao;
import org.malred.hotelsystem.model.entity.HotelEntity;

public class ManageUpdateActivity extends BaseActivity {
    EditText etTitle;
    EditText etDesc;
    EditText etNum;
    Button submitBtn;

    @Override
    protected int initLayout() {
        return R.layout.activity_manage_update;
    }

    @Override
    protected void initView() {
        etTitle = findViewById(R.id.upt_title);
        etDesc = findViewById(R.id.upt_desc);
        etNum = findViewById(R.id.upt_num);

        submitBtn = findViewById(R.id.upt_submit);
    }

    @Override
    protected void initData() {
        String title = (String) intentExtras.get("h_title");
        etTitle.setText(title);

        String desc = (String) intentExtras.get("h_desc");
        etDesc.setText(desc);

        int num = Integer.parseInt((String) intentExtras.get("h_num"));
        etNum.setText(num + "");

        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HotelEntity hotel = new HotelEntity();
                hotel.setId((String) intentExtras.get("h_id"));
                hotel.setTitle(String.valueOf(etTitle.getText()));
                hotel.setDescription(String.valueOf(etDesc.getText()));
                hotel.setNumber(Integer.parseInt(String.valueOf(etNum.getText())));
                boolean res = HotelDao.upt(getWritableDb(), hotel);
                if(res){
                    navigateTo(ManagerActivity.class);
                    showToastSync("修改成功");
                }else{
                    showToastSync("修改失败");
                }
            }
        });
    }
}